﻿
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System;
using UnityEngine;
using UnityEngine.UI;
using LitJson;

public class Client : MonoBehaviour
{

    //public Button sendBtn;//发送给服务器按钮
    //public Button connectBtn;//连接服务器按钮
    TcpSocket tcpClient;
    public int index;//该客户端编号
    private static Client instance;

    public static Client Instance
    {
        get
        {
            return instance;
        }
    }

    private void Awake()
    {
        DontDestroyOnLoad(this.gameObject);
        instance = this;
    }
    // Use this for initialization
    void Start()
    {
        Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        tcpClient = new TcpSocket(client, 1024, false);

    }

    // Update is called once per frame
    void Update()
    {
        if (tcpClient != null && tcpClient.ClientConnect())
        {
            tcpClient.ClientReceive();
        }
        DisposeMessage();
    }
    /// <summary>
    /// 链接服务器
    /// </summary>
    public void OnClickConnectBtn()
    {
        if (!tcpClient.ClientConnect())
        {
            tcpClient.ClientConnect(MessageManager.ip, MessageManager.port);
        }
    }
    /// <summary>
    /// 发送消息到服务器
    /// </summary>
    /// <param name="sendStr"></param>
    public void OnClickToSendServer(string sendStr)
    {
        if (tcpClient != null && tcpClient.ClientConnect() && !String.IsNullOrEmpty(sendStr))
        {
            tcpClient.ClientSend(sendStr);
        }
    }
    /// <summary>
    /// 获取操作携程
    /// </summary>
    /// <returns></returns>
    IEnumerator operationEnumerator()
    {
        while (true)
        {
            yield return new WaitForSeconds(0.03f);
            Info info = new Info();
            info.infoType = 3;
            info.operation.index = index;
            if (OperationController.Instance)
            {
                info.operation.frameIndex = OperationController.Instance.frameIndex;
                info.operation.operationInfo = OperationController.Instance.operationInfo;
            }
            
            string infoStr = JsonMapper.ToJson(info);
            infoStr += "&";
            OnClickToSendServer(infoStr);
            yield return new WaitForSeconds(0.07f);
        }
    }
    bool isStart = false;
    /// <summary>
    /// 处理接收到的消息
    /// </summary>
    public void DisposeMessage()
    {
        if (tcpClient.queue.Count != 0)
        {
            string message = (string)tcpClient.queue.Dequeue();
            Info info = JsonMapper.ToObject<Info>(message);
            switch (info.infoType)
            {
                case 0:
                    break;
                case 1:
                    index = info.operation.index;
                    GameController.LoadScene(SceneEnum.Game);
                    break;
                case 2:
                    break;
                case 3:
                    if (!isStart)
                    {
                        isStart = true;
                        StartCoroutine(nameof(operationEnumerator));
                    }
                    OperationController.Instance.ServerOperationSet(info.operation);
                    break;
                default:
                    break;
            }
        }
    }
}

